#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <set>
using namespace std;
class Solution
{
public:
    int longestOnes(vector<int> &A, int K)
    {
        int i = 0, j = 0, ans = 0, cnt = 0;
        while (j < A.size())
        {
            while (cnt > K)
                cnt -= (A[i++] == 0) ? 1 : 0;
            if (j < A.size() && A[j++] == 0)
                cnt++;
            ans = max(ans, j - i - (cnt > K ? 1 : 0));
        }
        return ans;
    }
};